package com.yh.hljapp.modules.adatper;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.hljapp.R;
import com.yh.hljapp.common.ArithUtil;
import com.yh.hljapp.common.StringUtils;
import com.yh.hljapp.component.dialog.DialogHelp;
import com.yh.hljapp.gddb.gdbean.MatBeanCount;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by imyar on 2016/5/3 0031.
 */
public class Sale_AddAdapter extends RecyclerView.Adapter<Sale_AddAdapter.SaleViewHolder> {

    private Context mContext;
    private ArrayList<MatBeanCount> workInfos;
    private String ftype;

    public Sale_AddAdapter(Context context, ArrayList<MatBeanCount> workInfos, OnSaleAddAdapterListern listener, String ftype) {
        mContext = context;
        this.workInfos = workInfos;
        this.listener = listener;
        this.ftype = ftype;
    }

    public List<MatBeanCount> getListMatBean() {
        return workInfos;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public SaleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sale_out, parent,
                false);
        return new SaleViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final SaleViewHolder holder, final int position) {

        if (holder instanceof SaleViewHolder) {
            final MatBeanCount bean = workInfos.get(position);
            if ("0".equals(ftype)) {
                holder.ll_sfsl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                        final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                        final EditText content = (EditText) view1.findViewById(R.id.et_content);
//                content.setInputType(InputType.TYPE_CLASS_NUMBER);
                        title.setText(bean.getName() + " -- " + "实发数量");
                        String s = holder.tv_sf_num.getText().toString().trim();
                        if (!StringUtils.isEmpty(s)) {
                            String[] split = s.split("=");
                            String s1 = split[0];
                            content.setText(s1);
                            content.setSelection(s1.length());
                        }
                        DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String trim = content.getText().toString().toString();

                                if (StringUtils.isEmpty(trim)) {
                                    holder.tv_sf_num.setText("0");
                                    trim = "0";
                                }
                            }
                        }).show();
                    }
                });

            } else {
                holder.tv_sf_num.setEnabled(false);
            }
            holder.ll_fhck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onFHCKClick(bean, position);
                    }
                }
            });
            holder.ll_bz.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                    final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                    final EditText content = (EditText) view1.findViewById(R.id.et_content);
                    content.setInputType(InputType.TYPE_CLASS_TEXT);
                    title.setText(bean.getName() + " -- " + "备注说明");

                }
            });
//            String value = MatHeadManager.getInstance(mContext).getHeadValue("发货仓库","2");

            String value = bean.getTDCStockName();
            String cusvalue = bean.getFDCStockName();//表头发货仓库
            if (!StringUtils.isEmpty(value)) {
                holder.tv_fh_ware.setText(value);//发货仓库
            } else {
                holder.tv_fh_ware.setText(cusvalue);//发货仓库
            }

            holder.tv_cc_code.setText(bean.getNumber());
            holder.tv_sf_num.setText(bean.getCount());

            holder.tv_gg_modle.setText(bean.getModel());
//            holder.tv_note.setText(bean.);//备注
            double v = (bean.getSaleprice()) * Integer.parseInt(bean.getCount());
            bean.setAllMoney(ArithUtil.round(v, 0));
            double qty = bean.getQty();
            holder.tv_unit.setText(bean.getUnit_name());
            holder.tv_cc_name.setText(bean.getName());

            ((SaleViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    DialogHelp.getConfirmDialog(mContext, "确定删除  " + bean.getName() + "  么？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            onItemDismiss(position);
                        }
                    }).show();
                    return true;
                }
            });
        }
    }

    private OnSaleAddAdapterListern listener;


    public void onItemDismiss(int position) {
        //删除mItems数据
        workInfos.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }

    public interface OnSaleAddAdapterListern {
        void onFHCKClick(MatBeanCount bean, int position);

        void onCountChanger(String id, String count);

        void onMoneyChanged();
    }

    @Override
    public int getItemCount() {
        return workInfos.size();
    }


    public class SaleViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_cc_code;
        private TextView tv_cc_name;
        private TextView tv_gg_modle;
        private TextView tv_unit;
        private TextView tv_sf_num;
        private TextView tv_fh_ware;
        private LinearLayout ll_fhck;
        private LinearLayout ll_sfsl;
        private LinearLayout ll_bz;


        public SaleViewHolder(View view) {
            super(view);
            tv_cc_code = (TextView) view.findViewById(R.id.tv_cc_code);
            tv_cc_name = (TextView) view.findViewById(R.id.tv_cc_name);
            tv_gg_modle = (TextView) view.findViewById(R.id.tv_gg_modle);
            tv_unit = (TextView) view.findViewById(R.id.tv_unit);
            tv_sf_num = (TextView) view.findViewById(R.id.tv_sf_num);
            tv_fh_ware = (TextView) view.findViewById(R.id.tv_fh_ware);
            ll_fhck = (LinearLayout) view.findViewById(R.id.ll_fhck);
            ll_sfsl = (LinearLayout) view.findViewById(R.id.ll_sfsl);
            ll_bz = (LinearLayout) view.findViewById(R.id.ll_bz);

        }
    }
}
